<template>
  <div>
    <base-list ref="stockManage"
               :condition="condition"
               :datas.sync="stockManageData"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               ref="aList"
               label-width="80px">
        <el-row>
          <el-col :span="24"
                  class="search-width4">
            <el-form-item label="管理方式">
              <el-select v-model="condition['search_EQ_manageWay']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="[key, val] in _dictManagement"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24"
                  class="search-width4">
            <el-form-item label="计量方式">
              <el-select v-model="condition['search_EQ_measureWay']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="[key, val] in _dictMeterStyle"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="货主">
              <auto-search-name placeholder="请选择"
                                @selectId="v=>condition['search_EQ_cargoOwnerId']=v"
                                :name="form.cargoOwnerName"></auto-search-name>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="跟踪号">
              <el-input type="text"
                        v-model="condition['search_LIKE_barcode']"
                        placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="品名">
              <el-select v-model="condition['search_EQ_cargoName']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="item in cargoNameList"
                           :key="item.cargoName"
                           :label="item.cargoName"
                           :value="item.cargoName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="材质">
              <auto-search-dic v-model="condition['search_EQ_material']"
                               @selectName="v=>condition['search_EQ_material']=v"
                               code="CARGO_CZ"></auto-search-dic>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="生产厂家"
                          clearable>
              <auto-search-dic v-model="condition['search_EQ_madeBy']"
                               @selectName="v=>condition['search_EQ_madeBy']=v"
                               code="CARGO_SCZ"></auto-search-dic>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="验收单号">
              <el-input type="text"
                        v-model="condition['search_LIKE_checkAcceptNo']"
                        placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="车船号">
              <el-input type="text"
                        v-model="condition['search_EQ_transhipNo']"
                        placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="冻结">
              <el-select v-model="condition['search_EQ_ifFrozen']"
                         placeholder="请选择">
                <el-option v-for="[key, val] in _dictTrueOrFalse"
                           :key="key"
                           :label="val"
                           :value="key">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="状态">
              <el-select v-model="condition['search_EQ_status']"
                         placeholder="请选择">
                <el-option v-for="[key, val] in _dictTrueOrFalse"
                           :key="key"
                           :label="val"
                           :value="key">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="成品">
              <el-select v-model="condition['search_EQ_ifEndproduct']"
                         placeholder="请选择">
                <el-option v-for="[key, val] in _dictOneOrZero"
                           :key="key"
                           :label="val"
                           :value="key">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="所属仓库">
              <el-select v-model="condition['search_EQ_warehouseCode']"
                         placeholder="请选择">
                <el-option v-for="(item,index) in warehouseList"
                           :key="index"
                           :label="item.name"
                           :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width4">
            <el-form-item label="库房">
              <auto-search-room-name @select="v=>condition['search_EQ_roomCode']=v"></auto-search-room-name>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width8">
            <el-form-item label="验收时间">
              <el-date-picker style="width:150px;"
                              v-model="condition['search_GTE_createDateTime']"
                              type="date"
                              placeholder="选择日期"
                              value-format="yyyy-MM-dd HH:mm:ss"> </el-date-picker> 至
              <el-date-picker style="width:150px;"
                              v-model="condition['search_LTE_createDateTime']"
                              type="date"
                              placeholder="选择日期"
                              value-format="yyyy-MM-dd HH:mm:ss"> </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width8">
            <el-form-item label="规格"
                          class="gg-input">
              <el-col :span="8">
                <auto-search-dic style="width: 100px;"
                                 @selectName="v=>condition['search_EQ_normOne']=v"
                                 code="CARGO_GGY"></auto-search-dic>
              </el-col>
              <el-col :span="8">
                <auto-search-dic style="width: 100px;"
                                 @selectName="v=>condition['search_EQ_normTwo']=v"
                                 code="CARGO_GGE"></auto-search-dic>
              </el-col>
              <el-col :span="8">
                <auto-search-dic style="width: 100px;"
                                 @selectName="v=>condition['search_EQ_normThree']=v"
                                 code="CARGO_GGS"></auto-search-dic>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="3"
                  style="width: 190px;">
            <el-button type="primary"
                       icon="el-icon-search"
                       @click="loadData"
                       class="ml-80">查 询</el-button>
            <el-button formVisible="true"
                       icon="el-icon-refresh"
                       @click="resuleBtn('aList')">
              重置
            </el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <div class="control-line">
              <!-- <div class="line"></div> -->
              账页列表
            </div>
          </el-col>
        </el-row>
      </el-form>
      <el-row slot="status">
        <el-button type="primary"
                   @click="frozen()">冻结货物</el-button>
        <el-button type="primary"
                   @click="unfrozen()">取消冻结</el-button>
        <el-button type="primary"
                   @click="handleDialogForm">调整库存</el-button>
      </el-row>
      <el-table slot="list"
                :data="stockManageData"
                border
                max-height="550"
                highlight-current-row
                @current-change="handleCurrentChange">
        <el-table-column type="expand"
                         width="60">
          <template slot-scope="props">
            <el-form label-position="left"
                     inline
                     class="demo-table-expand">
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="验收数量">
                  <span>{{ props.row.inCountNum}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="库存重量">
                  <span>{{ props.row.inventoryWeight}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="库存件数">
                  <span>{{ props.row.inventoryNum}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="库存数量">
                  <span>{{ props.row.inventoryCount}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="车船号">
                  <span>{{ props.row.transhipNo}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="验收单号">
                  <span>{{ props.row.checkAcceptNo}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="管理方式">
                  <span>{{ props.row.manageWay | _filterManagement}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="计量方式">
                  <span>{{ props.row.measureWay | _filterMeterStyle}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="磅次">
                  <span>{{ props.row.lbsTimes }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="合磅重量">
                  <span>{{ props.row.lbsWeight }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="吊牌重量">
                  <span>{{ props.row.tagWeight }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="数量单位">
                  <span>{{ props.row.numUnit | _filterCountNumUnitEnume}}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="实发重量">
                  <span>{{ props.row.factWeight }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="磅差重量">
                  <span>{{ props.row.lbsDiff }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="存单号">
                  <span>{{ props.row.stockBillNo }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="发货单号">
                  <span>{{ props.row.outStockPlanNo }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="提单号">
                  <span>{{ props.row.outStockEntrustNo }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="捆包号">
                  <span>{{ props.row.baleNo }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="炉批号">
                  <span>{{ props.row.stoveNo }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="6"
                      class="table-expand-style">
                <el-form-item label="运输方式">
                  <span>{{ props.row.transportWay | _filterTransportModeType }}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="验收人">
                  <span>{{ props.row.inMan}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="验收时间">
                  <span>{{ props.row.inTime}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="冻结人">
                  <span>{{ props.row.frozenMan}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="冻结时间">
                  <span>{{ props.row.frozenTime}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="冻结原因">
                  <span>{{ props.row.frozenReason}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="解冻人">
                  <span>{{ props.row.thawMan}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="解冻时间">
                  <span>{{ props.row.thawTime}}</span>
                </el-form-item>
              </el-col>
              <el-col class="table-expand-style">
                <el-form-item label="解冻原因">
                  <span>{{ props.row.thawReason}}</span>
                </el-form-item>
              </el-col>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="序号"
                         type="index"
                         width="50"></el-table-column>
        <el-table-column prop="status"
                         label="状态"
                         min-width="80">
          <template slot-scope='scope'>
            {{ scope.row.status | _filterStockStatus }}
          </template></el-table-column>
        <el-table-column prop="ifEndproduct"
                         label="成品"
                         min-width="120">
          <template slot-scope='scope'>
            {{ scope.row.ifEndproduct | _filterOneOrZero }}
          </template>
        </el-table-column>
        <el-table-column prop="ifFrozen"
                         label="冻结"
                         min-width="100">
          <template slot-scope='scope'>
            {{ scope.row.ifFrozen | _filterTrueOrFalse }}
          </template>
        </el-table-column>
        <el-table-column prop="cargoOwnerName"
                         label="货主"
                         min-width="150"></el-table-column>
        <el-table-column prop="trackingNo"
                         label="跟踪号"
                         min-width="80"></el-table-column>
        <el-table-column prop="id"
                         label="物质ID"
                         min-width="80"></el-table-column>
        <el-table-column prop="cargoName"
                         label="品名"
                         min-width="80"></el-table-column>
        <el-table-column prop="normStr"
                         label="规格"
                         min-width="80"></el-table-column>
        <el-table-column prop="material"
                         label="材质"
                         min-width="60"></el-table-column>
        <el-table-column prop="madeBy"
                         label="生产厂家"
                         min-width="80"></el-table-column>
        <el-table-column prop="warehouseName"
                         label="仓库"
                         min-width="80"></el-table-column>
        <el-table-column prop="roomName"
                         label="库房"
                         min-width="80"></el-table-column>
        <el-table-column prop="roomLocation"
                         label="库位"
                         min-width="120"></el-table-column>
        <el-table-column prop="num"
                         label="验收件数"
                         min-width="80"></el-table-column>
        <el-table-column prop="weight"
                         label="验收重量"
                         min-width="80"></el-table-column>
      </el-table>
    </base-list>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 plain
                 @click="handleRouterPush({path: '/goods/matter',name: '实物管理工作台'})">返回</el-button>
    </div>
    <adjust-stock-form ref="adjustStockForm"
                       @load="loadData"></adjust-stock-form>
  </div>
</template>

<script>
import {
  getCargoOwnerAccount,
  getCargoNameList
} from '@/api/things/accountPageList'
import {
  getStockManageList,
  frozenStock,
  unfrozenStock
} from '@/api/matter/stockManage'
import adjustStockForm from './adjustStockForm'
import autoSearchName from '@/components/autocomplete/autoSearchName' //货主组件
import autoCargo from '@/components/autocomplete/autoCargo'
import autoSearchDic from '@/components/autocomplete/autoSearchDic'
import autoSearchRoomName from '@/components/autocomplete/autoSearchRoomName'
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于仓库设置

export default {
  name: 'stockManage',
  components: {
    adjustStockForm,
    autoCargo,
    autoSearchName,
    autoSearchDic,
    autoSearchRoomName
  },
  data() {
    return {
      form: this.clearForm(),
      dialogForm: false,
      component: '',
      condition: {}, // 查询条件
      stockManageData: [], // table数组
      pmLIst: [], //接口获取的货物属性数据
      warehouseList: [],
      pageSearch: getStockManageList, // 分页查询api接口
      cargoOwnerAccount: [],
      cargoNameList: [],
      multipleSelection: [],
      currentRow: null
    }
  },
  mounted() {
    if (
      this.$route.query.spanNo ||
      this.$route.query.roomCode ||
      this.$route.query.warehouseCode ||
      this.$route.query.aulaNo
    ) {
      this.condition.search_EQ_aulaNo = this.$route.query.aulaNo
      this.condition.search_EQ_spanNo = this.$route.query.spanNo
      this.condition.search_EQ_roomCode = this.$route.query.roomCode
      this.condition.search_EQ_warehouseCode = this.$route.query.warehouseCode
    }
    this.$nextTick(() => {
      this.loadData()
    })
    //获取货主
    getCargoOwnerAccount(this.$store.state.user.currentUser.org.id).then(
      res => {
        this.cargoOwnerAccount = res.data.aaData
      }
    )
    //获取品名
    getCargoNameList(this.$store.state.user.currentUser.org.id).then(res => {
      this.cargoNameList = res.data.aaData
    })
    //获取仓库
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    })
  },
  methods: {
    clearForm() {
      return {}
    },
    loadData() {
      this.$refs.stockManage.loadData()
    },
    //单选
    handleCurrentChange(val) {
      this.currentRow = val
    },
    //调整库存
    handleDialogForm() {
      if (this.currentRow) {
        this.$refs.adjustStockForm.form = this.currentRow
        this.$refs.adjustStockForm.dialogForm = true
      } else {
        this.$message({ message: '请至少选中一条数据', type: 'error' })
      }
    },
    //货物冻结
    frozen() {
      if (this.currentRow) {
        this.$prompt('冻结原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
          .then(({ value }) => {
            frozenStock(this.currentRow.id, { reason: value }).then(res => {
              this.$message({
                type: 'success',
                message: '冻结成功!'
              })
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '取消输入'
            })
          })
      } else {
        this.$message({ message: '请至少选中一条数据', type: 'error' })
      }
    },
    //取消冻结
    unfrozen() {
      if (this.currentRow) {
        this.$confirm(`是否取消冻结?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            unfrozenStock(this.currentRow.id, { reason: '' }).then(res => {
              this.$message({
                type: 'success',
                message: `解冻成功!`
              })
              this.loadData()
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      } else {
        this.$message.error('请至少选择一条数据')
      }
    },
    //重置
    resuleBtn(aList) {
      this.$refs[aList].resetFields()
      this.condition = this.clearForm()
    },
    //返回
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style>
.control-line {
  height: 30px;
}
.line {
  border-left: solid 5px #00a1ff;
  height: 18px;
  margin-top: 5px;
  display: inline-block;
  vertical-align: sub;
}
.gg-input {
  margin-bottom: 0 !important;
}
.gg-input .el-form-item__content .el-input {
  width: 100px !important;
}
.search-width6 .el-date-editor {
  width: 240px;
}
.base-list .search .border-line {
  margin-top: 0px !important;
}
.table-expand-style {
  width: 300px;
}
.table-expand-style label {
  color: #99a9bf;
}
</style>
